<?php


namespace WenRuns\Laravel\Admin\Grid\Exporters;


use Encore\Admin\Admin;

class ExportButton extends \Encore\Admin\Grid\Tools\ExportButton
{

    public function render()
    {
        /**
         * @var $exporter \WenRuns\Laravel\Admin\Grid\Exporters\WenExporter|mixed
         */
        $exporter = $this->grid->getExportInstance();
        if ($exporter && (!method_exists($exporter, 'hasPermission') || $exporter->hasPermission())) {
            return parent::render(); // TODO: Change the autogenerated stub
        }
        return '';
    }

    protected function setUpScripts()
    {
        $exporter = $this->grid->getExportInstance();
        if (is_string($exporter)) {
            $exporter = new $exporter($this->grid);
        }
        $script = $exporter->setUpScripts();
        if (empty($script)) {
            $script = <<<SCRIPT
$('.{$this->grid->getExportSelectedName()}').click(function (e) {
    e.preventDefault();
    var rows = $.admin.grid.selected().join();
    if (!rows) {
        return false;
    }
    var href = $(this).attr('href').replace('__rows__', rows);
    window.open(href,"_blank");
});
SCRIPT;
        }

        Admin::script($script);
    }
}
